<?php

/**
 * This is the model class for table "task".
 *
 * The followings are the available columns in table 'task':
 * @property string $id_task
 * @property integer $id_client
 * @property integer $id_contact
 * @property integer $mail_sent_contact
 * @property integer $id_transporter
 * @property integer $id_tpdetail
 * @property integer $id_attribute
 * @property string $id_by_matter
 * @property string $delivery_date
 * @property string $deadline
 * @property string $responsible
 * @property string $note
 * @property integer $status
 * @property string $subject
 * @property string $client_reference
 * @property string $conten_mail_client
 * @property string $date_add
 * @property string $date_upd
 * @property integer $del_flg
 */
class Task extends CActiveRecord
{
    public $quantity;
    public $close_sms_to_contact;
    public $close_sms_content;
    public $close_email_to_contact;
    public $close_email_content;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Task the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'task';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_client, id_contact, id_tpdetail, id_attribute, id_by_matter, delivery_date, deadline, responsible', 'required'),
            array('status', 'required', 'on'=>'update'),
			array('id_client, id_contact, mail_sent_contact, id_tpdetail, id_attribute, status, del_flg, statusby', 'numerical', 'integerOnly'=>true),
			array('id_by_matter', 'length', 'max'=>20),
			array('responsible, subject', 'length', 'max'=>255),
			array('client_reference', 'length', 'max'=>700),
			array('note, conten_mail_client, date_add, date_upd, responsible, id_transporter', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id_task, id_client, id_contact, mail_sent_contact, id_transporter, id_tpdetail, id_attribute, id_by_matter, delivery_date, deadline, responsible, note, status, subject, conten_mail_client, client_reference, date_add, date_upd, del_flg, statusby', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'task_analyst' => array(self::HAS_MANY, 'TaskAnalyst', 'id_task'),
            'attribute_matter' => array(self::BELONGS_TO, 'Attribute', 'id_attribute'),
            'client' => array(self::BELONGS_TO, 'Client', 'id_client'),
            'cp' => array(self::BELONGS_TO, 'ContactPerson', 'id_contact'),
            'td' => array(self::BELONGS_TO, 'TransporterDetail', 'id_tpdetail'),
            'transport' => array(self::BELONGS_TO, 'Transporter', 'id_transporter'),
            'tracking_task' => array(self::HAS_ONE, 'TrackingTask', 'id_task'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id_task' => '#',
            'quantity' => Yii::t('app', 'Quantity'),
			'id_client' => Yii::t('app', 'Client'),
			'id_contact' => Yii::t('app', 'Contact person'),
			'mail_sent_contact' => Yii::t('app', 'Allow sent mail to contact'),
			'id_transporter' => Yii::t('app', 'Transport company'),
			'id_tpdetail' => Yii::t('app', 'Transporter'),
			'id_attribute' => Yii::t('app', 'Matter'),
			'id_by_matter' => Yii::t('app', 'Code matter'),
			'delivery_date' =>  Yii::t('app', 'Delivery Date'),
			'deadline' => Yii::t('app', 'Analyst Deadline'),
			'responsible' => Yii::t('app', 'Analyst Responsible'),
			'note' => Yii::t('app', 'Comment'),
			'status' => Yii::t('app', 'Status'),
			'subject' => Yii::t('app', 'Subject'),
			'conten_mail_client' => Yii::t('app', 'Conten Mail Client'),
			'client_reference' => Yii::t('app', 'Client reference number'),
			'date_add' => Yii::t('app', 'Date add'),
			'date_upd' => Yii::t('app', 'Last update'),
			'statusby' => Yii::t('app', 'Update status by'),
			'del_flg' => 'Del Flg',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
        $criteria->order = 'date_add DESC';
		$criteria->compare('id_task',$this->id_task,true);
		$criteria->compare('id_client',$this->id_client);
		$criteria->compare('id_contact',$this->id_contact);
		$criteria->compare('id_transporter',$this->id_transporter);
		$criteria->compare('id_tpdetail',$this->id_tpdetail);
		$criteria->compare('id_attribute',$this->id_attribute);
		$criteria->compare('id_by_matter',$this->id_by_matter,true);
		$criteria->compare('delivery_date',$this->delivery_date,true);
		$criteria->compare('deadline',$this->deadline,true);
		$criteria->compare('responsible',$this->responsible,true);
        $criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
            'pagination'=>array(
                'pageSize'=>SystemConfig::PAGE_SIZE,
            ),
			'criteria'=>$criteria,
		));
	}

    public function search_by_user()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
        $criteria->order = 'date_add DESC';
        $criteria->addCondition('responsible = :responsible');
        $criteria->params = array(':responsible' => Yii::app()->user->getState('username'));
        $criteria->compare('id_task',$this->id_task,true);
        $criteria->compare('id_client',$this->id_client);
        $criteria->compare('id_contact',$this->id_contact);
        $criteria->compare('id_transporter',$this->id_transporter);
        $criteria->compare('id_tpdetail',$this->id_tpdetail);
        $criteria->compare('id_attribute',$this->id_attribute);
        $criteria->compare('id_by_matter',$this->id_by_matter,true);
        $criteria->compare('delivery_date',$this->delivery_date,true);
        $criteria->compare('deadline',$this->deadline,true);
        $criteria->compare('note',$this->note,true);
        $criteria->compare('status',$this->status);

        return new CActiveDataProvider($this, array(
            'pagination'=>array(
                'pageSize'=>SystemConfig::PAGE_SIZE,
            ),
            'criteria'=>$criteria,
        ));
    }

    public function beforeSave() {
        $now = new CDbExpression('NOW()');
        if ($this->isNewRecord) $this->date_add = $now;

        $this->date_upd = $now;

        return parent::beforeSave();
    }

    public static function getListTask(){
        $criteria=new CDbCriteria;
        $criteria->addCondition('del_flg = 0');
        $criteria->addCondition('status = :status');
        $criteria->params = array(':status' => Constant::STATUS_CLOSE);

        $modelTask = Task::model()->findAll($criteria);
        $result = array();
        if ($modelTask) {
            foreach ($modelTask as $task) {
                $result[$task->id_task] = $task->attribute->name;
            }
        }

        return $result;
    }

    public static function _getTaskByClient($id_client){
        $criteria=new CDbCriteria;
        $criteria->addCondition('id_client = :id_client');
        $criteria->addCondition('status = :status');
        $criteria->addCondition('del_flg = 0');
        $criteria->params = array(':id_client' => (int)$id_client, ':status' => Constant::STATUS_CLOSE);
        $taskModel = Task::model()->findAll($criteria);
        if($taskModel){
            return $taskModel;
        }else{
            return false;
        }
    }
}